#include"Rect.h"

/////////////////
//include
/////////////////
#include"../CXBurst/StanderdIf.h"
using namespace CXBurst::StandardIf;
/////////////////
//prottype
/////////////////

/////////////////
//const
/////////////////


Rect::Rect(float x,float y, float width,float height)
{
	this->x = x;
	this->y = y;
	this->width		= width;
	this->height	= height;
}
Rect::Rect(Vector2D start,Vector2D end)
{
	Vector2D tmp_start = start;
	Vector2D tmp_end = end;
	if(start.x>end.x)
	{
		tmp_start.x = end.x;
		tmp_end.x  = start.x;
	}
	if(start.y>end.y)
	{
		tmp_start.y = end.y;
		tmp_end.y  = start.y;
	}
	Vector2D tmp_vec	= tmp_start+((tmp_end - tmp_start)/2);
	x					= tmp_vec.x;
	y					= tmp_vec.y;
	width				= ((tmp_end - tmp_start)/2).x*2;
	height				= ((tmp_end - tmp_start)/2).y*2;
}
Rect::~Rect()
{
}
bool Rect::Collision(const Rect& rect)
{
	return Collision(rect.GetRightUp())||Collision(rect.GetLeftUP())||Collision(rect.GetRightDown())||Collision(rect.GetLeftDown());
}
bool Rect::Collision(const Vector2D& point)
{
	return BitWeen<float>(point.x,x-width/2,x+width/2) && BitWeen<float>(point.y,y-height/2,y+height/2);
}
CollisionInfo Rect::GetCollisionInfo(const Rect& rect)
{
	return CollisionInfo(false,Vector2D(),RectDirection::Up);
}
Vector2D Rect::CollisionPos(const Rect& rect,Vector2D pos,Vector2D pre_pos)
{
	if(!Collision(rect))
		return Vector2D::GetWrongParameter();

	Line2D tmp_line = Line2D(pre_pos,pos);
	Line2D tmp_rects[4] ;
	tmp_rects[0]	=	Line2D(rect.GetRightUp(),rect.GetRightUp()		+tmp_line.GetDirection());
	tmp_rects[1]	=	Line2D(rect.GetLeftUP(),rect.GetLeftUP()		+tmp_line.GetDirection());
	tmp_rects[2]	=	Line2D(rect.GetLeftDown(),rect.GetLeftDown()	+tmp_line.GetDirection());
	tmp_rects[3]	=	Line2D(rect.GetRightDown(),rect.GetRightDown()	+tmp_line.GetDirection());
	Line2D tmp_rects2[4] ;
	tmp_rects2[0]	=	Line2D(GetRightUp(),GetLeftUP());
	tmp_rects2[1]	=	Line2D(GetLeftUP(),GetLeftDown());
	tmp_rects2[2]	=	Line2D(GetLeftDown(),GetRightDown());
	tmp_rects2[3]	=	Line2D(GetRightDown(),GetRightUp());
	float tmp_length = FLT_MAX;
	Vector2D tmp_collision;
	int tmp_num = -1;
	for(int i = 0;i<4;i++)
		for(int j = 0;j<4;j++)
		{
			tmp_collision = tmp_rects[i].CollisionPoint(tmp_rects2[j]);
			if(tmp_collision != Vector2D::GetWrongParameter())
			{
				if(tmp_length >(tmp_collision-tmp_rects[i].start).Length())
				{
					tmp_length = (tmp_collision-tmp_rects[i].start).Length();
					tmp_num = j;
				}
			}
		}
		if(tmp_num == -1)
			return Vector2D::GetWrongParameter();

		switch(tmp_num)
		{
		case 0:
				return pre_pos + tmp_line.GetDirectionNormalize() * (tmp_length)+Vector2D(0,0);
			break;
		case 1:
				return pre_pos + tmp_line.GetDirectionNormalize() * (tmp_length)+Vector2D(0,0);
			break;
		case 2:
				return pre_pos + tmp_line.GetDirectionNormalize() * (tmp_length)+Vector2D(0,0);
			break;
		case 3:
				return pre_pos + tmp_line.GetDirectionNormalize() * (tmp_length)+Vector2D(0,0);
			break;

		}
		return pre_pos + tmp_line.GetDirectionNormalize() * (tmp_length);

}
